// 导航守卫

import router from '@/router'
import store from '@/store'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const pageArr = ['/404', '/login']
router.beforeEach(async(to, from, next) => {
  Nprogress.start()
  if (store.getters.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
        next(to.fullPath)
      } else {
        next()
      }
    }
  } else {
    if (pageArr.includes(to.path.toLowerCase())) {
      next()
    } else {
      next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
    }
  }

  Nprogress.done()
})

router.afterEach((to, from) => {})
